package com.example.homeworkmutualevaluation.Controller;

import com.example.homeworkmutualevaluation.Component.ScheduledTaskController;
import com.example.homeworkmutualevaluation.Entity.Homework;
import com.example.homeworkmutualevaluation.Entity.Teacher;
import com.example.homeworkmutualevaluation.Service.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentService studentService;
    @Resource
    private CourseService courseService;
    @Resource
    private HomeworkService homeworkService;
    @Resource
    private TaskService taskService;
    @Resource
    private SubmitService submitService;
    @Resource
    private ScheduledTaskController scheduledTaskController;
    @Resource
    private RefanswerService refanswerService;
    @Resource
    private MessageService messageService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String phone, @RequestParam String password){
        HashMap<String, Object> response = new HashMap<>();
        if (teacherService.exist(phone)) {
            Teacher teacher = teacherService.find(phone, password);
            if (teacher != null) {
                response.put("code", 1);
                response.put("msg", "登入成功");
                response.put("teacher", teacher);
            } else {
                response.put("code", 0);
                response.put("msg", "密码错误");
            }
        } else {
            response.put("code", 0);
            response.put("msg", "账号不存在");
        }
        return response;
    }

    @PostMapping("/register")
    public Map<String, Object> register(@RequestParam String phone, @RequestParam String password){
        HashMap<String, Object> response = new HashMap<>();
        if (teacherService.exist(phone)) {
            response.put("code", 0);
            response.put("msg", "该手机号已存在");
        } else if (teacherService.register(phone, password)) {
            response.put("code", 1);
            response.put("msg", "注册成功");
        } else {
            response.put("code", 0);
            response.put("msg", "未知错误");
        }
        return response;
    }

    @PostMapping("/change_password")
    public Map<String, Object> changePassword(@RequestParam String phone,
                                              @RequestParam String oldPassword,
                                              @RequestParam String newPassword){
        HashMap<String, Object> response = new HashMap<>();
        if (teacherService.changePassword(phone, oldPassword, newPassword)) {
            response.put("code", 1);
            response.put("msg", "更改成功");
        } else {
            response.put("code", 0);
            response.put("msg", "密码错误");
        }
        return response;
    }

    @PostMapping("/get_info")
    public Map<String, Object> getInfo(@RequestParam String phone){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("teacher", teacherService.findByPhone(phone));
        return response;
    }

    @PostMapping("/update_info")
    public Map<String, Object> updateInfo(String phone, String name, String sex, String birthday) {
        HashMap<String, Object> response = new HashMap<>();
        Date birthdayData;
        try {
            birthdayData = Date.valueOf(birthday);
        } catch (java.lang.IllegalArgumentException e) {
            response.put("code", 0);
            response.put("msg", "日期格式错误或日期不合法(必须为yyyy-mm-dd)");
            return response;
        }
        if (!(sex.equals("m") || sex.equals("f"))) {
            response.put("code", 0);
            response.put("msg", "性别只能是m或f");
        } else if (teacherService.updateInfo(phone, name, sex, birthdayData)) {
            response.put("code", 1);
            response.put("msg", "更改成功");
        } else {
            response.put("code", 0);
            response.put("msg", "教师不存在");
        }
        return response;
    }

    @PostMapping("/teaching_course")
    public HashMap<String, Object> teachingCourse(@RequestParam String tphone){
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", courseService.findAllByTphone(tphone));
        return response;
    }

    @PostMapping("/assign")
    public Map<String, Object> assign(@RequestParam String cno,
                                      @RequestParam String title,
                                      @RequestParam String content,
                                      @RequestParam String deadline,
                                      @RequestParam String scoretime){
        HashMap<String, Object> response = new HashMap<>();
        Timestamp deadlineTimestamp, scoreTimestamp;
        try {
            deadlineTimestamp = Timestamp.valueOf(deadline);
            scoreTimestamp = Timestamp.valueOf(scoretime);
        } catch (java.lang.IllegalArgumentException e) {
            response.put("code", 0);
            response.put("msg", "时间格式错误或时间不合法(必须为yyyy-mm-dd hh:mm:ss)");
            return response;
        }
        if (!deadlineTimestamp.before(scoreTimestamp)) {
            response.put("code", 0);
            response.put("msg", "出成绩时间必须在提交时间之后");
        } else {
            int homeworkid = homeworkService.insert(cno, title, content, deadlineTimestamp, scoreTimestamp);
            scheduledTaskController.add(ScheduledTaskController.ScheduledTask.StartEvaluation, homeworkid, deadlineTimestamp);
            scheduledTaskController.add(ScheduledTaskController.ScheduledTask.EndEvaluation, homeworkid, scoreTimestamp);
            response.put("code", 1);
            response.put("msg", "作业发布成功");
            response.put("homeworkid", homeworkid);
        }
        return response;
    }

    @PostMapping("/change_homework")
    public Map<String, Object> changeHomework(@RequestParam int homeworkid,
                                              @RequestParam String title,
                                              @RequestParam String content,
                                              @RequestParam String deadline,
                                              @RequestParam String scoretime){
        HashMap<String, Object> response = new HashMap<>();
        Timestamp deadlineTimestamp, scoreTimestamp;
        try {
            deadlineTimestamp = Timestamp.valueOf(deadline);
            scoreTimestamp = Timestamp.valueOf(scoretime);
        } catch (java.lang.IllegalArgumentException e) {
            response.put("code", 0);
            response.put("msg", "时间格式错误或时间不合法(必须为yyyy-mm-dd hh:mm:ss)");
            return response;
        }
        Homework homework = homeworkService.findById(homeworkid);
        if (!deadlineTimestamp.before(scoreTimestamp)) {
            response.put("code", 0);
            response.put("msg", "出成绩时间必须在提交时间之后");
        } else {
            taskService.deleteAllByHomeworkid(homeworkid);
            homework.setTitle(title);
            homework.setContent(content);
            homework.setDeadline(deadlineTimestamp);
            homework.setScoretime(scoreTimestamp);
            homeworkService.save(homework);
            scheduledTaskController.add(ScheduledTaskController.ScheduledTask.StartEvaluation, homeworkid, deadlineTimestamp);
            scheduledTaskController.add(ScheduledTaskController.ScheduledTask.EndEvaluation, homeworkid, scoreTimestamp);
            response.put("code", 1);
            response.put("msg", "作业更新成功");
        }
        return response;
    }

    @PostMapping("/get_submitters")
    public Map<String, Object> getSubmitters(@RequestParam int homeworkid) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1);
        response.put("data", submitService.findAllSubmitter(homeworkid));
        return response;
    }

    @PostMapping("/set_refanswer")
    public Map<String, Object> setRefanswer(@RequestParam int homeworkid, @RequestParam String content) {
        HashMap<String, Object> response = new HashMap<>();
        refanswerService.setContent(homeworkid, content);
        response.put("code", 1);
        response.put("msg", "参考答案设置成功");
        return response;
    }

    @PostMapping("/send_message")
    public Map<String, Object> sendMessage(String tphone, String sphone, String title, String content) {
        HashMap<String, Object> response = new HashMap<>();
        if (!teacherService.exist(tphone)) {
            response.put("code", 0);
            response.put("msg", "教师不存在");
        } else if (!studentService.exist(sphone)) {
            response.put("code", 0);
            response.put("msg", "学生不存在");
        } else {
            messageService.sendMsg(tphone, sphone, title, content);
            response.put("code", 1);
        }
        return response;
    }

    @PostMapping("/send_messages")
    public Map<String, Object> sendMessages(String tphone, List<String> sphone, String title, String content) {
        HashMap<String, Object> response = new HashMap<>();
        if (!teacherService.exist(tphone)) {
            response.put("code", 0);
            response.put("msg", "教师不存在");
        } else {
            int count = messageService.sendMsg(tphone, sphone, title, content);
            response.put("code", 1);
            response.put("count", count);
        }
        return response;
    }
}
